package com.streamer.app.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.streamer.app.R
import com.streamer.app.bean.WinningRecordBean

class WinningRecordAdapter(
    private val records: MutableList<WinningRecordBean>
) : RecyclerView.Adapter<WinningRecordAdapter.RecordVH>() {

    class RecordVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvLotteryName: TextView = itemView.findViewById(R.id.tv_lottery_name)
        val tvWinnerName: TextView = itemView.findViewById(R.id.tv_winner_name)
        val tvPrizeName: TextView = itemView.findViewById(R.id.tv_prize_name)
        val tvWinTime: TextView = itemView.findViewById(R.id.tv_win_time)
        val tvStatus: TextView = itemView.findViewById(R.id.tv_status)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecordVH {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_winning_record, parent, false)
        return RecordVH(view)
    }

    override fun onBindViewHolder(holder: RecordVH, position: Int) {
        val record = records[position]
        
        holder.tvLotteryName.text = record.lotteryName
        holder.tvWinnerName.text = "中奖者：${record.winnerName}"
        holder.tvPrizeName.text = "奖品：${record.prizeName}"
        holder.tvWinTime.text = "中奖时间：${record.winTime}"
        holder.tvStatus.text = record.status
        
        // 根据状态设置不同的背景色
        when (record.status) {
            "已发放" -> {
                holder.tvStatus.setBackgroundResource(R.drawable.view_confirm_bg)
                holder.tvStatus.setTextColor(holder.itemView.context.getColor(R.color.white))
            }
            "待发放" -> {
                holder.tvStatus.setBackgroundResource(R.drawable.btn_blue_background)
                holder.tvStatus.setTextColor(holder.itemView.context.getColor(R.color.white))
            }
            "已过期" -> {
                holder.tvStatus.setBackgroundResource(R.drawable.btn_gray_background)
                holder.tvStatus.setTextColor(holder.itemView.context.getColor(R.color.gray))
            }
        }
    }

    override fun getItemCount(): Int = records.size

    fun addRecord(record: WinningRecordBean) {
        records.add(0, record) // 添加到列表开头
        notifyItemInserted(0)
    }

    fun addRecords(newRecords: List<WinningRecordBean>) {
        val startPosition = records.size
        records.addAll(newRecords)
        notifyItemRangeInserted(startPosition, newRecords.size)
    }

    fun updateRecords(newRecords: List<WinningRecordBean>) {
        records.clear()
        records.addAll(newRecords)
        notifyDataSetChanged()
    }

    fun clearRecords() {
        records.clear()
        notifyDataSetChanged()
    }
} 